<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class module_class extends AWS_MODEL
{

	/**
	 * 热门问题
	 */
	function hot_questions()
	{
		$hot_questions = $this->data_cache('get_question_get_hot_v2_' . $this->user_id . '_10', '$this->model("question")->get_question_list(false,"", 5,"answer_count desc"," add_time<' . (time() - 60 * 60 * 24 * 30) . ' ")', get_setting('cache_level_high'));
		
		if ($hot_questions)
		{
			foreach ($hot_questions as $key => $val)
			{
				if ($this->model('question')->has_focus_question($val['question_id'], $this->user_id))
				{
					$hot_questions[$key]['has_focus'] = true;
				}
				else
				{
					$hot_questions[$key]['has_focus'] = false;
				}
			}
		}
		
		return $hot_questions;
	
	}

	/**
	 * 可能感兴趣的人和话题
	 * @return multitype:
	 */
	function recommend_users_topics($uid)
	{
		$recommend_users = $this->model("account")->get_user_recommend_v2($uid, 20);
		
		$recommend_topics = $this->model("topic")->get_user_recommend_v2($uid, 20);
		
		if (! is_array($recommend_topics))
		{
			return array_slice($recommend_users, 0, get_setting('recommend_users_number'));
		}
		
		if ($recommend_topics)
		{
			shuffle($recommend_topics);
			$recommend_topics = array_slice($recommend_topics, 0, intval(get_setting('recommend_users_number') / 2));
		}
		
		if ($recommend_users)
		{
			shuffle($recommend_users);
			$recommend_users = array_slice($recommend_users, 0, (get_setting('recommend_users_number') - count($recommend_topics)));
		}
		
		if (! is_array($recommend_users))
		{
			$recommend_users = array();
		}
		
		if (! is_array($recommend_topics))
		{
			$recommend_topics = array();
		}
		
		$recommend_users_topics = array_merge($recommend_users, $recommend_topics);
		
		return $recommend_users_topics;
	}

	/**
	 * 边栏最新动态
	 */
	function sidebar_new_dynamic()
	{
		
		$new_topic = $this->model('topic')->get_topic_list('', 10);
		
		if ($new_topic)
		{
			foreach ($new_topic as $k => $v)
			{
				$new_topic[$k]['topic_title'] = cjk_substr($v['topic_title'], 0, 12, 'UTF-8', '...');
				$new_topic[$k]['topic_title_full'] = $v['topic_title'];
			}
		}
		
		$new_list = $this->model("question_topic")->get_question_list(10);
		
		return array(
			"new_topic" => $new_topic, 
			"new_list" => $new_list
		);
	}

	/**
	 * 边栏分类
	 * @return 
	 */
	function sidebar_category()
	{
		$sidebar_category = $this->model('system')->fetch_category("question");
		
		return $sidebar_category;
	}

	/**
	 * 内容分类
	 * @return 
	 */
	function content_category()
	{
		$content_category = $this->model('system')->fetch_category("question");
		
		return $content_category;
	}

	/**
	 * 边栏热门话题
	 */
	function sidebar_hot_topics($uid = 0, $category = 0)
	{
		$topics = $this->model('topic')->get_hot_topic($_GET['category'], 0, 5);
		
		if (is_array($topics['topics']))
		{
			foreach ($topics['topics'] as $key => $val)
			{
				$topics['topics'][$key]['has_focus'] = $this->model('topic')->has_focus_topic($uid, $val['topic_id']);
			
			}
			
			return $topics['topics'];
		}
		else
		{
			
			return array();
		}
	
	}

	/**
	 * 边栏热门用户
	 */
	function sidebar_hot_user2($uid = 0, $category = 0, $limit = 5)
	{
		$limit = intval($limit);
		
		$uid = intval($uid);
		$category = intval($category);
		$cache_key = "sidebar_hot_user_" . $category . '_' . $limit;
		$data = ZCACHE::get($cache_key);
		
		$hot_user_period = get_setting('hot_user_period');
		
		if (! $hot_user_period)
		{
			$hot_user_period = 30;
		}
		
		if ($data === false)
		{
			$min_time = time() - 60 * 60 * 24 * $hot_user_period;
			
			if ($category > 0)
			{
				$sql = "SELECT uid FROM " . $this->get_table("answer") . " WHERE category_id = " . $category . " AND add_time > " . $min_time;
			}
			else
			{
				$sql = "SELECT uid FROM  " . $this->get_table("answer") . " WHERE add_time > " . $min_time;
			}
			
			if ($answer_rs = $this->query_all($sql))
			{
				foreach ($answer_rs as $key => $val)
				{
					if (! $user_answer_count[$val['uid']])
					{
						$user_answer_count[$val['uid']] = 1;
					}
					else
					{
						$user_answer_count[$val['uid']] ++;
					}
					
					if(!isset($uids[$val['uid']]))
					{
						$uids[$val['uid']]=$val['uid'];
					}
				}
			}
			
			if ($category > 0)
			{
				$user_answer_all_count = $this->count("answer", " category_id = " . $category . " AND add_time > " . $min_time);
				
				$sql = "SELECT answer_uid FROM " . $this->get_table("answer_vote") . " WHERE answer_id IN (SELECT answer_id FROM " . $this->get_table("answer") . " WHERE category_id = " . $category . " AND add_time > " . $min_time . " ) AND vote_value = 1";
			}
			else
			{
				$user_answer_all_count = $this->count("answer", "add_time > " . $min_time);
				
				$sql = "SELECT answer_uid FROM " . $this->get_table("answer_vote") . " WHERE answer_id IN (SELECT answer_id FROM " . $this->get_table("answer") . "  WHERE add_time > " . $min_time . " )  AND vote_value = 1";
			}
			
			if ($vote_rs = $this->query_all($sql))
			{
				foreach ($vote_rs as $key => $val)
				{
					if (! $user_vote_count[$val['answer_uid']])
					{
						$user_vote_count[$val['answer_uid']] = 1;
					}
					else
					{
						$user_vote_count[$val['answer_uid']] ++;
					}
				}
			}
			
			
			if(!$uids)
			{
				return array();
			}
			//取出所有用户
			$users_list = $this->model("account")->get_users_list('answer_count > 0 AND uid IN('.implode(',',$uids).') AND uid <> ' . $uid, null, false, false, null);
			
			//计算数组得分
			if ($users_list)
			{
				foreach ($users_list as $key => $val)
				{
					
					$user_answer_count[$val['uid']] = intval($user_answer_count[$val['uid']]);
					
					$user_vote_count[$val['uid']] = intval($user_vote_count[$val['uid']]);
					
					$scores = 0;
					
					if ($user_answer_count[$val['uid']])
					{
						$scores = $user_answer_count[$val['uid']] + $user_vote_count[$val['uid']];
					}
					
					//$scores = $scores + $val['online_time'] / 60 * 60 * 24 * get_setting('hot_user_period');
					
					if ($user_vote_count[$val['uid']]) //去掉赞成为0的
					{
						$a_user_list[$val['uid']] = array(
							"users" => $val, 
							"scores" => $scores
						);
					}
				}
			}
			
			if (is_array($a_user_list))
			{
				$a_user_list = aasort($a_user_list, array(
					"-scores"
				));
			}
			
			//数组排序
			if ($a_user_list)
			{
				$i = 0;
				
				foreach ($a_user_list as $key => $val)
				{
					$i ++;
					
					if ($i > $limit)
					{
						break;
					}
					
					if (intval($user_answer_count[$val['users']['uid']]) < 1)
					{
						continue;
					}
					
					$hot_user_list[$key] = $val['users'];
					$hot_user_list[$key]['focus'] = $this->model('follow')->user_follow_check($uid, $val['users']['uid']);
					$hot_user_list[$key]['answer_count'] = intval($user_answer_count[$val['users']['uid']]);
					$hot_user_list[$key]['vote_count'] = intval($user_vote_count[$val['users']['uid']]);
				}
			}
			
			ZCACHE::set($cache_key, $hot_user_list, null, get_setting('cache_level_high'));
			
			return $hot_user_list;
		}
		else
		{
			return $data;
		}
	}
}